<!DOCTYPE html>
<html>
	<head th:include="/common/head :: head">
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit|ie-comp|ie-stand">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
		<meta http-equiv="Cache-Control" content="no-siteapp" />
	</head>
<body>
	<div class="x-body">
		<form class="layui-form" lay-filter="update_form">
			<div class="layui-form-item">
				<label for="userName" class="layui-form-label"> <span
					class="x-red">*</span>登陆名
				</label>
				<div class="layui-input-inline">
					<input type="text" id="userName" name="userName" required=""
						lay-verify="userName" autocomplete="off" class="layui-input"
						readOnly="readOnly">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="nickName" class="layui-form-label"> <span
					class="x-red">*</span>昵称
				</label>
				<div class="layui-input-inline">
					<input type="text" id="nickName" name="nickName" required=""
						lay-verify="nickName" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="mobile" class="layui-form-label"> <span
					class="x-red">*</span>手机
				</label>
				<div class="layui-input-inline">
					<input type="text" id="mobile" name="mobile" required=""
						lay-verify="phone" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="email" class="layui-form-label"> <span
					class="x-red">*</span>邮箱
				</label>
				<div class="layui-input-inline">
					<input type="text" id="email" name="email" required=""
						lay-verify="email" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="password" class="layui-form-label"> <span
					class="x-red">*</span>密码
				</label>
				<div class="layui-input-inline">
					<input type="text" id="firstPassword" name="password" required=""
						lay-verify="password" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="confirmPass" class="layui-form-label"> <span
					class="x-red">*</span>确认密码
				</label>
				<div class="layui-input-inline">
					<input type="text" id="confirmPass" name="confirmPass" required=""
						lay-verify="confirmPass" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">性别</label>
				<div class="layui-input-block">
					<input type="radio" name="sex" value="1" title="男"> 
					<input type="radio" name="sex" value="2" title="女">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="L_repass" class="layui-form-label"> </label>
				<button class="layui-btn" lay-filter="update_form" lay-submit="">保存</button>
			</div>
		</form>
	</div>
</body>
</html>
<script>
	$(function(){
		//初始化
		init();
	})
	var _this={};
	var updateFormData ={};
	//根据id获取用户信息
	var userId = sessionStorage.getItem("userId");
	//初始化
	function init(){
		$.post("/user/getUserById","userId="+userId,function(result){
			if(result.status==0){
				//初始化修改表单
				//cosole.log(result);
				initUpdateForm(result.data);
			}else{
				layer.msg(result.msg);
			}
		})
	}
	//初始化修改表单
	function initUpdateForm(user){
		layui.use(['form','layer'],function(){
			$ = layui.jquery;
			var form = layui.form,
			layer = layui.layer;
			//自定义校验
			form.verify({
				nickName:function(value){
					if(value.length<3){
						return "昵称至少得3个字符";
					}
				},
				password:function(value){
					if(value!=null&&value!=""){
						if(value.length<6){
							return "密码长度不能小于6位";
						}
					}
				},
				confirmPass:function(value){
					if($('#firstPassword').val()!=value){
						return "两次密码不一致,须一致";
					}
				}
			})
			//表单初始赋值
			form.val('update_form',{
				"userName": user.userName ,
				"nickName": user.nickName,
				"mobile": user.mobile,
				"email": user.email
			})
			if(user.sex==1){
				form.val('update_form', {
					"sex": "1"
				})
			}else{
				form.val('update_form', {
					"sex": "2"
				})
			}
			//监听提交
			form.on("submit(update_form)",function(data){
				console.log(data.field);
				updateFormData = data.field;
				updateUser();
				return false;
			})
		})
	}
	//提交修改信息
	function updateUser(){
		$.ajax({
			url:"/user/updateUser",
			type:"post",
			data:{
				"userId":userId,
				"nickName":updateFormData.nickName,
				"mobile":updateFormData.mobile,
				"email":updateFormData.email,
				"password":updateFormData.password,
				"confirmPass":updateFormData.confirmPass,
				"sex":updateFormData.sex
			},
			success:function(result){
				if(result.status==0){
					layer.msg(result.msg,function(){
						//关闭弹出层
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);
						//刷新当前页
						//parent.location.replace(parent.location.href);
					})
				}else{
					layer.msg(result.msg);
				}
			}
		})
	}

</script>
